<html>
    <head>
        <title>pham thanh anh chau</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="../public/CSS/db.css">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>
    <body>
        <h1 align="center">Pham Thanh Anh Chau</h1>
        <br>
        <?php require_once '../config/pages.php'; ?>
        <?php
        $query = "SELECT * FROM companies";
        $companies = getFilms($query);
        $query = "SELECT * FROM apps";
        $apps = getFilms($query);
        ?>

        <form  class="form1" method="POST" align="center">
            Keyword  : <input type="text" name="keyword" id="keyword" value="cafe">
            Company<select name="id_company">
                <option value="0">Company</option>
                <?php foreach ($companies as $company): ?>
                    <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
                <?php endforeach; ?>
            </select>
            App<select name="id_app">
                <option value="0">Application</option>
                <?php foreach ($apps as $app): ?>
                    <option value="<?php echo $app['id'] ?>"><?php echo $app['name'] ?></option>
                <?php endforeach; ?>
            </select>
            <input type="text" name="min" id="min" placeholder="from" value="1">
            <input type="text" name="max" id="max" placeholder="to" value="10">
            <input type="button" value="check" id="check">
            <input type="submit" name="submit" value="Search">

        </form>

        <?php if (isset($_POST['submit'])): ?>
            <?php
            $min = $_POST['min'];
            $max = $_POST['max'];
            $keyword = $_POST['keyword'];
            $company = $_POST["id_company"];
            $app = $_POST["id_app"];
            if (!empty($company))
                $company = "(companies.id='$company')AND";
            else
                $company = "";
            if (!empty($app))
                $app = "(apps.id='$app' )AND";
            else
                $app = "";
            if (empty($max) && !empty($min)) {
                $query = "SELECT * FROM pages
        INNER JOIN apps_pages ON pages.id=apps_pages.id_page
        INNER JOIN apps ON apps.id=apps_pages.id_app
        INNER JOIN companies ON companies.id=apps.id_company
            WHERE
                  $company
                  $app
                  (pages.likes>= $min )AND
                  (pages.description LIKE '% $keyword %')
                  LIMIT 0,10
                  ";
            } else if (!empty($max) && empty($min)) {
                $query = "SELECT * FROM pages
        INNER JOIN apps_pages ON pages.id=apps_pages.id_page
        INNER JOIN apps ON apps.id=apps_pages.id_app
        INNER JOIN companies ON companies.id=apps.id_company
            WHERE
                  $company
                  $app
                  (pages.likes>= $max )AND
                  (pages.description LIKE '% $keyword %')
                  LIMIT 0,10
                  ";
            } else if (empty($max) && empty($min)) {
                $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
                WHERE
                  $company
                  $app
                  (pages.description LIKE '% $keyword %')
                  LIMIT 0,10
                ";
            } else {
                $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
                WHERE
                  $company
                  $app
                  (pages.likes>= $min) AND
                  (pages.likes<= $max) AND
                  (pages.description LIKE '% $keyword %')
                  LIMIT 0,10
                ";
            }
            var_dump($query);
            $films = getFilms($query);
            ?>
        <?php else: ?>
            <?php
            $query = "SELECT * FROM pages LIMIT 0,10";
            $films = getFilms($query)
            ?>
        <?php endif; ?>
        <?php if (!empty($films)): ?>
            <table id="nav" border="1">
                <tr class="tieude1">
                    <th class="pid">pid</th>
                    <th class="id">id</th>
                    <th class="name">name</th>
                    <th class="username">username</th>
                    <th class="phone">phone</th>
                    <th class="likes">likes</th>
                    <th class="talk">talking_about_count</th>
                    <th class="cate">category</th>
                    <th class="link">link</th>
                    <th class="web">website</th>
                    <th class="pic">picture</th>
                    <th class="country">country</th>
                    <th class="des">description</th>
                </tr>
                <?php foreach ($films as $film): ?>
                    <tr class="tieude2">
                        <td align="center"><?php echo $film["pid"] ?></td>
                        <td align="center"><?php echo $film["id"] ?></td>
                        <td align="center"><?php echo $film["name"] ?></td>
                        <td align="center"><?php echo $film["username"] ?></td>
                        <td align="center"><?php echo $film["phone"] ?></td>
                        <td align="center"><?php echo $film["likes"] ?></td>
                        <td align="center"><?php echo $film["talking_about_count"] ?></td>
                        <td align="center"><?php echo $film["category"] ?></td>
                        <td align="center"><a href='<?php echo $film["link"] ?>'><?php echo $film["link"] ?></td>
                        <td align="center"><a href='<?php echo $film["website"] ?>'><?php echo $film["website"] ?></td>
                        <td align="center"><img src ='<?php echo $film["picture"] ?>'</td>
                        <td align="center"><?php echo $film["country"] ?></td>
                        <td ><?php echo substr($film["description"], 0, 100) ?></td>
                    </tr>
                <?php endforeach; ?>
            </table>
        <?php endif; ?>
<!--        <script type="text/javascript">
    var txtmin = document.getElementById("min").value;
    var txtmax = document.getElementById("max").value;
    var txtkw = document.getElementById("keyword").value;
    alert("min : " + txtmin + "\nmax : " + txtmax + "\nkeyword : " + txtkw);
</script>
<script>
    $("#min").click(function() {
        alert($("#min").val());
    });
    $("#max").click(function() {
        alert($("#max").val());
    });
    $("#keyword").click(function() {
        alert($("#keyword").val());
    });
</script>-->
        <script>
            $(document).ready(function() {
                $('#check').click(function() {
                    var min = $('#min').val();
                    var max = $('#max').val();
                    var flag2 = checkNumber(max);
                    var flag1 = checkNumber(min);
                    if (flag1 && flag2)
                        alert('is number');
                    else
                        alert('is not number');
                });
            });
            function checkNumber(val) {
                if ($.isNumeric(val))
                    return true;
                else
                    return false;
            }
        </script>
    </body>
</html>